<?php
namespace app\cash\controller;
use think\Controller;
use think\Db;
use app\cash\model\OrderDetails;
use app\cash\model\StoreGoodsLog;
class Refund extends Controller{
    /**
     * @Author  lingyun
     * @Desc    退款
     * 2018-06-12
     */
    public function index(){
        $order_id = input('order_id');          //订单ID

        $store = db('store')->where('id',session('cash_user')['store_id'])->find();

        if($store['is_jiameng'] == 1){          //是加盟商
            $order = db('order')->where('id',$order_id)->find();        //订单详情
            if(empty($order)){      //订单为空
                $this->error('找不到订单信息，不能发起退款','');
            }else if($order['is_tuikuan'] == 1){        //已退款不能重复退款
                $this->error('该订单已退款，请勿重复操作','');
            }else if($order['pay_status'] == 0){
                $this->error('该订单暂未支付，不能退款','');
            }

            if($order['pay_type'] == 3){        //余额支付
                $member = db('user')->where('uid',$order['uid'])->find();
                $user_wallet_log = [
                    'uid'=>$order['uid'],
                    'admin_id'=>session('cash_user')['id'],
                    'store_id'=>$order['store_id'],
                    'order_id'=>$order['id'],
                    'order_sn'=>$order['order_sn'],
                    'pay_status'=>1,
                    'pay_type'=>$order['pay_type'],
                    'amount'=>$order['final_amount'],
                    'cur_amount'=>$member['user_money']+$order['final_amount'],
                    'sign'=>1,
                    'chargetype'=>3,
                    'charge_cn'=>'订单退款',
                    'addtime'=>time(),
                    'is_deleted'=>0,
                ];

                db('user_wallet_log')->insert($user_wallet_log);
                db('user')->where('uid',$order['uid'])->setInc('user_money',$order['final_amount']);        //会员余额增加
            }

            db('order')->where('id',$order['id'])->update(['order_status'=>6,'remark'=>'订单已退款','tuikuan_time'=>time(),'is_tuikuan'=>1]);     //更新订单状态
            $order_action_log = [
                'uid'=>$order['uid'],
                'order_id'=>$order['id'],
                'action_user'=>session('cash_user')['id'],
                'order_status'=>6,
                'pay_status'=>1,
                'status_desc'=>'订单已退款',
                'storeid'=>$order['store_id'],
                'addtime'=>time(),
            ];

            db('order_action')->insert($order_action_log);
            $refund_log = [
                'uid'=>$order['uid'],
                'order_id'=>$order['id'],
                'order_sn'=>$order['order_sn'],
                'amount'=>$order['final_amount'],
                'casher'=>session('cash_user')['id'],
                'addtime'=>time()
            ];

            db('order_refund')->insert($refund_log);

            //门店金额记录
            $store_credit_log = [
                'store_id'=>$order['store_id'],
                'order_id'=>$order['id'],
                'type'=>3,
                'credit'=>$order['final_amount'],
                'cur_credit'=>$store['total_amount']-$order['final_amount'],
                'admin_id'=>session('cash_user')['id'],
                'remark'=>'订单退款',
                'addtime'=>time(),
                'order_type'=>1,
                'order_sn'=>$order['order_sn'],
                'pay_type'=>$order['pay_type']
            ];
            db('store_credit_log')->insert($store_credit_log);      //保存数据

            if ($order['pay_type'] == 1) {
                $fild = 'al_money';
            } else if ($order['pay_type'] == 2) {
                $fild = 'wx_money';
            } else if ($order['pay_type'] == 3) {          //余额
                $fild = 'user_money';
            } else if ($order['pay_type'] == 4) {
                $fild = 'xianjin_money';
            } else if ($order['pay_type'] == 5) {
                $fild = 'bank_money';
            } else if ($order['pay_type'] == 6) {
                $fild = 'meituan_money';
            } else if ($order['pay_type'] == 7) {
                $fild = 'elema_money';
            }

            if($order['pay_type'] == 3){
                $update_arr = [
                    'total_amount'=>$store['total_amount']-$order['final_amount'],
                    'credit'=>$store['credit'] - $order['final_amount'],
                    'user_money'=>$store['user_money'] - $order['final_amount']
                ];
            }else{
                $update_arr = [
                    'total_amount'=>$store['total_amount']-$order['final_amount'],
                    $fild=>$store[$fild] - $order['final_amount']+$order['pay_type_two'],
                    'user_money'=>$store['user_money'] - $order['pay_type_two']
                ];
            }

            db('store')->where('id',$store['id'])->update($update_arr);         //更新门店金额


            $pay_log = [
                'order_id'=>$order['id'],
                'order_sn'=>$order['order_sn'],
                'order_type'=>empty($order['order_type'])?1:2,
                'uid'=>$order['uid'],
                'pay_type'=>$order['pay_type'],
                'pay_sign'=>4,
                'amount'=>$order['final_amount'],
                'remark'=>'订单退款',
                'addtime'=>time(),
                'store_id'=>$order['store_id']
            ];
            db('pay_log')->insert($pay_log);

            if($order['pay_type'] == 3){
                $this->success('退款成功，金额已返还到会员卡余额','');
            }else{
                $this->success('退款成功，请线下支付会员退款金额','');
            }
        }else{          //不是加盟商
            $order = db('order')->where('id',$order_id)->find();        //订单详情

            if(empty($order)){      //订单为空
                $this->error('找不到订单信息，不能发起退款','');
            }else if($order['is_tuikuan'] == 1){        //已退款不能重复退款
                $this->error('该订单已退款，请勿重复操作','');
            }else if($order['pay_status'] == 0){
                $this->error('该订单暂未支付，不能退款','');
            }
            if($order['pay_type'] == 3){            //余额付款
                $member = db('user')->where('uid',$order['uid'])->find();
                $user_wallet_log = [
                    'uid'=>$order['uid'],
                    'admin_id'=>session('cash_user')['id'],
                    'store_id'=>$order['store_id'],
                    'order_id'=>$order['id'],
                    'order_sn'=>$order['order_sn'],
                    'pay_status'=>1,
                    'pay_type'=>$order['pay_type'],
                    'amount'=>$order['final_amount'],
                    'cur_amount'=>$member['user_money']+$order['final_amount'],
                    'sign'=>1,
                    'chargetype'=>3,
                    'charge_cn'=>'订单退款',
                    'addtime'=>time(),
                    'is_deleted'=>0,
                ];

                db('user_wallet_log')->insert($user_wallet_log);
                db('user')->where('uid',$order['uid'])->setInc('user_money',$order['final_amount']);        //会员余额增加
                db('order')->where('id',$order['id'])->update(['order_status'=>6,'remark'=>'订单已退款','tuikuan_time'=>time(),'is_tuikuan'=>1]);     //更新订单状态
                $order_action_log = [
                    'uid'=>$order['uid'],
                    'order_id'=>$order['id'],
                    'action_user'=>session('cash_user')['id'],
                    'order_status'=>6,
                    'pay_status'=>1,
                    'status_desc'=>'订单已退款',
                    'storeid'=>$order['store_id'],
                    'addtime'=>time(),
                ];
                db('order_action')->insert($order_action_log);
                $refund_log = [
                    'uid'=>$order['uid'],
                    'order_id'=>$order['id'],
                    'order_sn'=>$order['order_sn'],
                    'amount'=>$order['final_amount'],
                    'casher'=>session('cash_user')['id'],
                    'addtime'=>time()
                ];
                db('order_refund')->insert($refund_log);

                //门店金额记录
                $store_credit_log = [
                    'store_id'=>$order['store_id'],
                    'order_id'=>$order['id'],
                    'type'=>3,
                    'credit'=>$order['final_amount'],
                    'cur_credit'=>$store['total_amount']-$order['final_amount'],
                    'admin_id'=>session('cash_user')['id'],
                    'remark'=>'订单退款',
                    'addtime'=>time(),
                    'order_type'=>1,
                    'order_sn'=>$order['order_sn'],
                    'pay_type'=>$order['pay_type']
                ];
                db('store_credit_log')->insert($store_credit_log);      //保存数据
                $update_arr = [
                    'total_amount'=>$store['total_amount']-$order['final_amount'],
                    'credit'=>$store['credit'] - $order['final_amount'],
                    'user_money'=>$order['final_amount']
                ];
                db('store')->where('id',$store['id'])->update($update_arr);         //更新门店金额

                $pay_log = [
                    'order_id'=>$order['id'],
                    'order_sn'=>$order['order_sn'],
                    'order_type'=>empty($order['order_type'])?1:2,
                    'uid'=>$order['uid'],
                    'pay_type'=>$order['pay_type'],
                    'pay_sign'=>4,
                    'amount'=>$order['final_amount'],
                    'remark'=>'订单退款',
                    'addtime'=>time(),
                    'store_id'=>$order['store_id']
                ];
                db('pay_log')->insert($pay_log);

                $this->success('退款成功，金额已返还到会员卡余额','');
            }else if($order['pay_type'] == 4 || $order['pay_type'] == 5 || $order['pay_type'] == 6 || $order['pay_type'] == 7){
                db('order')->where('id',$order['id'])->update(['order_status'=>6,'remark'=>'订单已退款','tuikuan_time'=>time(),'is_tuikuan'=>1]);     //更新订单状态
                $order_action_log = [
                    'uid'=>$order['uid'],
                    'order_id'=>$order['id'],
                    'action_user'=>session('cash_user')['id'],
                    'order_status'=>6,
                    'pay_status'=>1,
                    'status_desc'=>'订单已退款',
                    'storeid'=>$order['store_id'],
                    'addtime'=>time(),
                ];
                db('order_action')->insert($order_action_log);
                $refund_log = [
                    'uid'=>$order['uid'],
                    'order_id'=>$order['id'],
                    'order_sn'=>$order['order_sn'],
                    'amount'=>$order['final_amount'],
                    'casher'=>session('cash_user')['id'],
                    'addtime'=>time()
                ];
                db('order_refund')->insert($refund_log);

                //门店金额记录
                $store_credit_log = [
                    'store_id'=>$order['store_id'],
                    'order_id'=>$order['id'],
                    'type'=>3,
                    'credit'=>$order['final_amount'],
                    'cur_credit'=>$store['total_amount']-$order['final_amount'],
                    'admin_id'=>session('cash_user')['id'],
                    'remark'=>'订单退款',
                    'addtime'=>time(),
                    'order_type'=>1,
                    'order_sn'=>$order['order_sn'],
                    'pay_type'=>$order['pay_type']
                ];
                db('store_credit_log')->insert($store_credit_log);      //保存数据
                if ($order['pay_type'] == 1) {
                    $fild = 'al_money';
                } else if ($order['pay_type'] == 2) {
                    $fild = 'wx_money';
                } else if ($order['pay_type'] == 3) {          //余额
                    $fild = 'user_money';
                } else if ($order['pay_type'] == 4) {
                    $fild = 'xianjin_money';
                } else if ($order['pay_type'] == 5) {
                    $fild = 'bank_money';
                } else if ($order['pay_type'] == 6) {
                    $fild = 'meituan_money';
                } else if ($order['pay_type'] == 7) {
                    $fild = 'elema_money';
                }

                $update_arr = [
                    'total_amount'=>$store['total_amount']-$order['final_amount'],
                    $fild=>$store[$fild] - $order['final_amount']+$order['pay_type_two'],
                    'user_money'=>$store['user_money'] - $order['pay_type_two']
                ];
                db('store')->where('id',$store['id'])->update($update_arr);         //更新门店金额

                $pay_log = [
                    'order_id'=>$order['id'],
                    'order_sn'=>$order['order_sn'],
                    'order_type'=>empty($order['order_type'])?1:2,
                    'uid'=>$order['uid'],
                    'pay_type'=>$order['pay_type'],
                    'pay_sign'=>4,
                    'amount'=>$order['final_amount'],
                    'remark'=>'订单退款',
                    'addtime'=>time(),
                    'store_id'=>$order['store_id']
                ];
                db('pay_log')->insert($pay_log);

                $this->success('退款成功，请线下支付会员退款金额','');
            }else if($order['pay_type'] == 1){          //支付宝
                $this->redirect(url('Alirefund/index').'?order_id='.$order_id);
            }else{
                $this->redirect(url('Wxrefund/index').'?order_id='.$order_id);
            }
        }
    }

}